package com.xinqizhi.service.impl;

import com.xinqizhi.entity.SysRoleMenu;
import com.xinqizhi.mapper.SysRoleMenuMapper;
import com.xinqizhi.service.SysRoleMenuService;
import com.xinqizhi.vo.RoleMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleMenuServiceImpl  implements SysRoleMenuService {
	/**
	* 已经实现基础的增、删、改、查、批量删除、批量修改、批量插入，
	*/
	@Autowired
	private SysRoleMenuMapper roleMenuMapper;


	/**
	 * 由roleId查询锁关联的菜单,并判断判断关联状态
	 *
	 * @param roleId
	 * @return
	 */
	@Override
	public List<RoleMenuVo> getRole2MenuByRoleId(Integer roleId) {

		//查询所有菜单角色关联状态
		List<RoleMenuVo> roleMenuStatus= roleMenuMapper.selectMenusByRoleId(roleId);

		return roleMenuStatus;
	}

    /**
     * 为角色批量添加关联菜单
     * @param roleId
     * @param mids
     * @return
     */
	@Override
	public Integer addRoleIdByMenuLsit(Integer roleId,List<Integer> mids) {
		SysRoleMenu roleMenu=null;
		List<SysRoleMenu> roleMenus = new ArrayList<>();
		for (Integer v:mids) {
			roleMenu=new SysRoleMenu();
			roleMenu.setMenuId(v);
			roleMenu.setRoleId(roleId);
			roleMenu.setRoleMenuStatus(1);
			roleMenu.setRoleMenuDisc(roleId.toString());
			roleMenus.add(roleMenu);
		}
		return roleMenuMapper.replaceBatchData(roleMenus);
	}


    /**
     * 批量删除角色和菜单的关联
     * @param roleMenuVos
     * @return
     */
	@Override
	public Integer batchDeleteRoleMenu(List<RoleMenuVo> roleMenuVos) {
		Integer[] rmIds= new Integer[roleMenuVos.size()];
		int i=0;
        for (RoleMenuVo v:roleMenuVos) {
            rmIds[i]=v.getRoleMenuId();
            i++;
        }
		return roleMenuMapper.deleteBatchData(rmIds);
	}

	/**
	 * 通过角色id查询该角色已经有的菜单Id
	 *
	 * @param roleId
	 * @return
	 */
	@Override
	public List<Integer> selectMenuIdByRoleId(Integer roleId) {
		return roleMenuMapper.selectMenuIdByRoleId(roleId);
	}


}
